const path = require("path");
const fs = require("fs");
const https = require("https");
const source = require("./source.json");
const sharp = require("sharp");

async function downLoadImage(url, saveDir) {
  const { filename, image } = await new Promise((resolve) => {
    https.get(url, (image) => {
      const disposition = image.headers["content-disposition"];
      const reg = /filename=\"(.*?)\"/;
      const [match, filename] = (disposition || "").match(reg) || [];
      if (filename) {
        resolve({ filename, image });
      }
    });
  });
  console.log("filename: ", filename);

  const _path = path.join(saveDir, filename);

  await new Promise((resolve, reject) => {
    const writer = fs.createWriteStream(_path);
    image.pipe(writer);
    writer.on("finish", () => {
      writer.close();
      resolve();
    });
    writer.on("error", reject);
  });

  return sharp(_path).metadata();
}
const downImages = source.reduce((rets, url) => {
  const _url = new URL(url);
  if (_url.pathname === "/search/down") {
    return [...rets, url];
  }
  return rets;
}, []);

// downLoadImage(downImages[1], "D:/图库/高清").then((data) => {
//   console.log("data: ", data);
// });

async function downLoadImages(urls, len = 4) {
  const pools = new Set();
  for (let n = 0; n < urls.length; n++) {
    const promise = downLoadImage(urls[n], "D:/图库/壁纸")
      .then((metadata) => {
        console.log("download success!", `width:${metadata.width}`);
      })
      .catch((error) => {
        console.log("download fail!");
      })
      .finally(() => {
        pools.delete(promise);
      });
    pools.add(promise);
    if (pools.size >= len) {
      await Promise.race(pools);
    }
  }
  await Promise.all(pools);
  console.log("download end!");
}
downLoadImages(downImages.slice(0, 100));

// [...document.getElementsByTagName('a')].map(item=>item.href) // 在浏览器中执行之后，把图片地址复制到source.json文件中
